package com.hand.wfive.view.dialog;

import android.content.Context;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hand.wfive.R;
import com.hand.wfive.activity.PrivacyActivity;
import com.hand.wfive.entity.WordDownOrderModel;
import com.hand.wfive.loginAndVip.UserManager;
import com.hand.wfive.loginAndVip.ui.BuyKnowDialog;
import com.hand.wfive.util.click.NoFastClickUtils;
import com.lxj.xpopup.core.BottomPopupView;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;

public class WordDownNoticeView extends BottomPopupView {
    @BindView(R.id.buyKnowCheck)
    CheckBox buyKnowCheck;
    @BindView(R.id.payWayWechat)
    RadioButton payWayWechat;
    @BindView(R.id.totalPrice)
    TextView totalPrice;
    @BindView(R.id.tv_price_all)
    TextView tv_price_all;
    @BindView(R.id.tv_imagesize)
    TextView tv_imagesize;
    @BindView(R.id.tv_memberPrice)
    TextView tv_memberPrice;
    @BindView(R.id.tv_normalPrice)
    TextView tv_normalPrice;
    protected OnWordDownCallBack onWordDownCallBack;
    private WordDownOrderModel payModel;
    private String payPrice;

    public WordDownNoticeView(@NonNull Context context, WordDownOrderModel payModel, OnWordDownCallBack onWordDownCallBack) {
        super(context);
        this.payModel = payModel;
        this.onWordDownCallBack = onWordDownCallBack;
    }

    public interface OnWordDownCallBack {
        void payCall(String payPrice, String payType);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_word_down_notice;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ButterKnife.bind(this);
        String memberPrice = payModel.getData().getMemberPrice();
        String normalPrice = payModel.getData().getNormalPrice();
        boolean vip = UserManager.getInstance().isVip();
        BigDecimal multiply;
        if (vip) {
            multiply = new BigDecimal(memberPrice).multiply(new BigDecimal(payModel.getImageSize()));
        } else {
            multiply = new BigDecimal(normalPrice).multiply(new BigDecimal(payModel.getImageSize()));
        }
        tv_price_all.setText(multiply.toString());
        totalPrice.setText(multiply.toString());
        payPrice = multiply.toString();
        tv_imagesize.setText("当前文档为" + payModel.getImageSize() + "页，价格：");

        tv_normalPrice.setText(normalPrice);
        tv_memberPrice.setText(memberPrice);
    }

    @OnClick({R.id.buyKnow, R.id.bt_to_buy, R.id.iv_close})
    public void onClick(View view) {
        if (!NoFastClickUtils.isFastClick()) {
            switch (view.getId()) {
                case R.id.buyKnow: {
                    PrivacyActivity.showRule(this.getContext(), PrivacyActivity.VIPRULE);
                }
                break;
                case R.id.bt_to_buy: {
                    if (buyKnowCheck.isChecked()) {
                        if (onWordDownCallBack != null)
                            if (payWayWechat.isChecked())
                                onWordDownCallBack.payCall(payPrice, "wxpay");
                            else
                                onWordDownCallBack.payCall(payPrice, "alipay");
                        dismiss();
                    } else {
                        new BuyKnowDialog(this.getContext(), new Function0<Unit>() {
                            @Override
                            public Unit invoke() {
                                buyKnowCheck.setChecked(true);
                                if (onWordDownCallBack != null)
                                    if (payWayWechat.isChecked())
                                        onWordDownCallBack.payCall(payPrice, "wxpay");
                                    else
                                        onWordDownCallBack.payCall(payPrice, "alipay");
                                dismiss();
                                return null;
                            }
                        }).show();
                    }

                }
                break;
                default: {
                    dismiss();
                }

            }
        }


    }
}
